<?php
namespace app\index\model;
use think\Model;

class Cate extends Model
{
    
   public function getchildren($cateid){
    	$cateres=$this->select();
    	$arr=$this->_getchildren($cateres,$cateid);
    	 $arr[]=$cateid;
    	 $strarr=implode(',', $arr);
    	 return $strarr;
    }

    public function _getchildren($cateres,$cateid){
    	static $arr=array();
    	foreach ($cateres as $k => $v) {
    		if($v['pid']== $cateid){
    			$arr[]=$v['id'];
    			$this->_getchildren($cateres,$v['id']);
    		}
    	}
    	return $arr;
    }

    //查询上级所有栏目
    public function getparentid($cateid){
    	$cateres=$this->select();
    	$cate=db('cate')->find($cateid);
    	$pid=$cate['pid'];
    	if($pid){
    		$arr=$this->_getparentid($cateres,$pid);
    	}
    	
    	 $arr[]=$cate;
    	 return $arr;
    	 //dump($arr);die;
    }

    public function _getparentid($cateres,$pid){
    	static $arr=array();
    	foreach ($cateres as $k => $v) {
    		if($v['id']== $pid){
    			$arr[]=$v;
    			$this->_getchildren($cateres,$v['pid']);
    		}
    	}
    	return $arr;
    }
}
